/**
 * 
 */
package edu.sjtu.se.prophet.io.gson;

import java.lang.reflect.Type;
import java.util.Map;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import edu.sjtu.se.prophet.graph.Node;

/**
 * @author willard
 * 
 */
public class NodeDeserializer implements JsonDeserializer<Node> {

    @Override
    public Node deserialize(JsonElement json, Type type,
            JsonDeserializationContext context) throws JsonParseException {
        Node node = new Node("to be replaced");
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject()
                .entrySet()) {
            if ("id".equals(entry.getKey())) {
                node.setId(entry.getValue().getAsString());
            } else if ("metrics".equals(entry.getKey())) {
                for (Map.Entry<String, JsonElement> m : entry.getValue()
                        .getAsJsonObject().entrySet()) {
                    node.getMetrics().put(m.getKey(),
                            context.deserialize(m.getValue(), Object.class));
                }
            }
        }
        return node;
    }

}
